<?php
require '../inc/basics.php';
hasPermission('accountManager','rolToewijzen');
?>
	<script type="text/javascript" >
			$(".multiple").selectable();
			
			$("#select_user_groups li").click(function () {
				$(this).addClass("ui-selected").siblings().removeClass("ui-selected");
				$("ul .ui-selected").each(function () {
					//get assigned users in group member pane
					$.getJSON("/a/f=adminAssignedUsers", {id : $(this).attr('id')}, function(data){
						$("#select_assigned_users").empty();
						var select = $("#select_assigned_users");
						$.each(data, function(key, val) {
							select.append("<li id="+val.id+">"+val.name+" "+val.surname+"</li>\n");
						});
					});					
					//get deassigned users in available users pane
					$.getJSON("/a/f=adminDeassignedUsers", {id : $(this).attr('id')}, function(data){
						$("#select_deassigned_users").empty();
						var select = $("#select_deassigned_users");
						$.each(data, function(key, val) {
							select.append("<li id="+val.id+">"+val.name+" "+val.surname+"</li>\n");
						});
					});	
				});
			});

			//assign role(s) to a user
			$(":button#add").click(function() {
				$("#select_deassigned_users .ui-selected").each(function () {
					$.getJSON("/a/f=adminAssignUser", {uid : $(this).attr('id'), rid : $("#select_user_groups .ui-selected").attr('id') }, function(data){
						if (data.succes == true) $("#select_deassigned_users .ui-selected").remove().appendTo('#select_assigned_users');
					});
				});
				return false;
			});
			//deassign role(s) to a user
			$(":button#remove").click(function() {
				$("#select_assigned_users .ui-selected").each(function () {
					$.getJSON("/a/f=adminDeassignUser", {uid : $(this).attr('id'), rid : $("#select_user_groups .ui-selected").attr('id') }, function(data){
						if (data.succes == true) $("#select_assigned_users .ui-selected").remove().appendTo('#select_deassigned_users');
					});
				});
				return false;
			});


	</script>
	
	<div class="inner-center">	
		<form>
			<table class="user_accounts">
				<tr><th colspan="3">Groups</th><th colspan="3">Available users</th><th>Assigned users</th></tr>
				<tr>
					<td colspan="2">
						<ul id="select_user_groups" class="selectable">
							<?php
							$administrator = Administrator::getInstance();
							$groups = $administrator->listRoles();
							foreach($groups as $group){
								echo "<li id=".$group['id'].">".  htmlspecialchars($group['name'])."</li>\n";
							}
							?>
						</ul>
					</td>
					<td width="30"></td>
					<td>
						<ul id="select_deassigned_users" class="selectable multiple"></ul>
					</td>
					<td width="15"></td><td width="50"><br><br><br><br><br><br><button id="add">></button><br><br><br><button id="remove"><</button></td>
					<td>
						<ul id="select_assigned_users" class="selectable multiple"></select>
					</td>
				</tr>
			</table>
		</form>
	</div>